// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <utility>
#include <cantor/parser/parser.h>
#include <cantor/transform/ctors/enum_arg.h>
#include <cantor/transform/clause/type_name_uri.h>
#include <cantor/transform/expr.h>

namespace cantor::transform {

    void TransEnumArg::resolve_ast(const cantor::parser::EnumArg*node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }
        if (node->uri) {
            status = TransTypeNameUri::resolve_to(node->uri, enum_arg.mutable_uri());
        }
        if (!status.ok()) {
            return;
        }
        if (node->is_field) {
            enum_arg.set_str_value(node->field.c_str());
        } else {
            enum_arg.set_int_value(node->value);
        }

        set_proto_location(node, enum_arg.mutable_location());
        set_proto_status(enum_arg.mutable_status());
    }

    const turbo::Status &TransEnumArg::check() {
        if (!status.ok()) {
            return status;
        }
        return status;
    }

    turbo::Status TransEnumArg::resolve_to(turbo::Nonnull<const cantor::parser::EnumArg*> node, turbo::Nonnull<pb::ast::EnumArg*>out) {
        TransEnumArg translator;
        translator.resolve_ast(node);
        auto status = translator.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(translator.enum_arg);
        return status;
    }
}  // namespace cantor::transform
